package com.hu.jie.mywallpager.activity;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.hu.jie.mywallpager.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;

public class HomeActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {
    private int position;
    private ViewPager home_viewpager;
    private ArrayList<ImageView> Images;
    private ArrayList<String> mDatas;
    private MyAdapter adapter;
    private DisplayImageOptions options;
    private PopupWindow popupWindow;
    private Toolbar toolbar;
    private MyApp ma;
    private Button bt1,bt2,bt3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seek);
        home_viewpager = (ViewPager) findViewById(R.id.home_viewpager);
        popupWindow = new PopupWindow();
        bt1 = (Button) findViewById(R.id.homebt1);
        bt2 = (Button) findViewById(R.id.homebt2);
        bt3 = (Button) findViewById(R.id.homebt3);
        toolbar = (Toolbar) findViewById(R.id.home_toolbar);
        options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .showImageOnFail(R.mipmap.ic_launcher)
                .showImageOnFail(R.mipmap.ic_launcher)
                .build();

        mDatas = new ArrayList<>();
        Images = new ArrayList<>();
        adapter = new MyAdapter();
        Intent intent = getIntent();
        //position = intent.getIntExtra("url",position);
        mDatas = intent.getStringArrayListExtra("mlist");
        Log.i("123","<---!!!----!!!----!!!-------->"+mDatas.size());
        Log.i("123","<-------------------------->"+position);
        initData();
        home_viewpager.setCurrentItem(position);

        home_viewpager.setAdapter(adapter);
        home_viewpager.addOnPageChangeListener(this);
        //showPopupView();

        //创建助手类对象
        ma = new MyApp(HomeActivity.this,"pager.db",null,1);
        SQLiteDatabase db = ma.getWritableDatabase();

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(HomeActivity.this, "点了？", Toast.LENGTH_SHORT).show();
                finish();
            }
        });


    }

    public class MyApp extends SQLiteOpenHelper{

        public MyApp(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
            super(context, name, factory, version);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("create table words(_id integer primary key,express_img BLOB)");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        }
    }
    public void btn_collect(View v){
        Toast.makeText(HomeActivity.this, "点了1", Toast.LENGTH_SHORT).show();
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();

        //获取数据库连接
        SQLiteDatabase db = ma.getWritableDatabase();
        //创建ContentValues对象，用于封装要插入的信息
        ContentValues cv = new ContentValues();

        cv.put("express_img", String.valueOf(mDatas));
        //执行insert插入到表中，插入数据封装在cv中
        db.insert("pager",null,cv);
        //关闭数据库
        db.close();
    }

    public void btn_wallpager(View v){
        Toast.makeText(HomeActivity.this, "点了2", Toast.LENGTH_SHORT).show();

    }

    public void btn_download(View v){
        Toast.makeText(HomeActivity.this, "点了3", Toast.LENGTH_SHORT).show();

    }


//    private void showPopupView() {
//        if (popupWindow.isShowing()){
//            //充气机填充为控件
//            popupWindow.setWidth(ViewPager.LayoutParams.WRAP_CONTENT);
//            popupWindow.setHeight(ViewPager.LayoutParams.WRAP_CONTENT);
//            View popupView = getLayoutInflater().inflate(R.layout.home_popwindows,null);
//            popupWindow = new PopupWindow(popupView, ViewPager.LayoutParams.MATCH_PARENT, ViewPager.LayoutParams.WRAP_CONTENT,true);
//            popupWindow.setFocusable(true);
//            popupWindow.setOutsideTouchable(true);
//            popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
//            popupWindow.showAtLocation(home_viewpager,Gravity.NO_GRAVITY,0,0);
//        }
//    }


    private void initData() {
        for (int i = 0; i < mDatas.size(); i++) {
            ImageView iv = new ImageView(getApplication());
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            Images.add(iv);
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        ImageLoader.getInstance().displayImage(mDatas.get(position),Images.get(position));
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    public class MyAdapter extends PagerAdapter{

       @Override
       public int getCount() {
           return mDatas.size();
       }

       @Override
       public boolean isViewFromObject(View view, Object object) {
           return view==object;
       }

       @Override
       public Object instantiateItem(ViewGroup container, int position) {
            View view = Images.get(position);
           container.addView(view);
           return view;
       }

       @Override
       public void destroyItem(ViewGroup container, int position, Object object) {
           container.removeView(Images.get(position));
       }
   }

}
